package com.sky.controller.admin;

import com.sky.dto.OrderPageQueryDTO;
import com.sky.dto.OrdersDTO;
import com.sky.entity.Order;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;  // 添加这行导入

import java.util.Map;

@RestController
@RequestMapping("/admin/order")
@Api(tags = "订单相关接口")
@Slf4j
public class OrderController {
    @Autowired
    private OrderService orderService;
    
    @PostMapping("/updateStatus")
    @ApiOperation("更新订单状态")
    public Result<String> updateStatus(@RequestBody Map<String, Object> params) {
        log.debug("开始更新订单状态，参数：{}", params);  // 添加调试信息
    
        String orderNo = (String) params.get("orderNo");
        String orderStatus = (String) params.get("orderStatus");  // 获取 orderStatus
    
        orderService.updateStatus(orderNo, orderStatus);
    
        log.debug("订单状态更新成功，订单号：{}", orderNo);  // 添加调试信息
        return Result.success();
    }

    @PostMapping("/upload")
    @ApiOperation("上传订单")
    public Result<String> upload(@RequestBody OrdersDTO ordersDTO) {
        log.info("上传订单：{}", ordersDTO);
        orderService.upload(ordersDTO);
        return Result.success();
    }

    @GetMapping("/page")
    @ApiOperation("订单分页查询")
    public Result<PageResult> page(OrderPageQueryDTO orderPageQueryDTO) {
        try {
            // 参数校验
            if (orderPageQueryDTO == null) {
                orderPageQueryDTO = new OrderPageQueryDTO();
            }
            if (orderPageQueryDTO.getPage() <= 0) {
                orderPageQueryDTO.setPage(1);
            }
            if (orderPageQueryDTO.getPageSize() <= 0) {
                orderPageQueryDTO.setPageSize(10);
            }

            
            // 执行查询
            PageResult pageResult = orderService.pageQuery(orderPageQueryDTO);

            
            return Result.success(pageResult);
        } catch (Exception e) {
            log.error("订单查询失败，异常信息：", e);
            return Result.error("订单查询失败：" + e.getMessage());
        }
    }

    @GetMapping("/paidCustomers")
    @ApiOperation("获取已付款客户列表")
    public Result<PageResult> getPaidCustomers(OrderPageQueryDTO orderPageQueryDTO) {
        log.info("已付款客户查询：{}", orderPageQueryDTO);
        orderPageQueryDTO.setStatus("已支付");
        PageResult pageResult = orderService.pageQuery(orderPageQueryDTO);
        return Result.success(pageResult);
    }

    @GetMapping("/pageinfo")
    @ApiOperation("小程序订单分页查询")
    public Result<PageResult> pageinfo(
            @RequestParam(required = false) Long userId,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        try {
            log.info("小程序订单查询，用户ID：{}，页码：{}，每页条数：{}", userId, pageNum, pageSize);
            
            // 构建查询参数
            OrderPageQueryDTO queryDTO = new OrderPageQueryDTO();
            queryDTO.setUserId(userId);
            queryDTO.setPage(pageNum);
            queryDTO.setPageSize(pageSize);
            
            // 执行查询
            PageResult pageResult = orderService.pageQuery(queryDTO);

            return Result.success(pageResult);
            
        } catch (Exception e) {
            log.error("小程序订单查询失败：", e);
            return Result.error("订单查询失败：" + e.getMessage());
        }
    }

    @GetMapping("/detail/{id}")
    @ApiOperation("获取订单详情")
    public Result<Order> getOrderDetail(@PathVariable Long id) {
        log.info("获取订单详情：{}", id);
        Order order = orderService.getOrderDetail(id);
        return Result.success(order);
    }

    @PutMapping("/{id}")
    @ApiOperation("更新订单信息")
    public Result<String> updateOrder(@PathVariable Long id, @RequestBody Order order) {
        log.info("更新订单信息：id={}, order={}", id, order);
        order.setId(id);
        orderService.updateOrder(order);
        return Result.success();
    }

    @PutMapping("/material-status")
    @ApiOperation("更新订单材料状态")
    public Result<String> updateMaterialStatus(@RequestBody Map<String, Object> params) {
        try {
            Long id = Long.valueOf(params.get("id").toString());
            String materialStatus = (String) params.get("materialStatus");
            
            log.info("更新订单材料状态：id={}, materialStatus={}", id, materialStatus);
            
            orderService.updateMaterialStatus(id, materialStatus);
            return Result.success();
        } catch (Exception e) {
            log.error("更新订单材料状态失败：", e);
            return Result.error("更新材料状态失败：" + e.getMessage());
        }
    }
    
    @PutMapping("/embassy-status")
    @ApiOperation("更新订单签证状态")
    public Result<String> updateVisaStatus(@RequestBody Map<String, Object> params) {
        try {
            Long id = Long.valueOf(params.get("id").toString());
            String visaStatus = (String) params.get("visaStatus");
            
            log.info("更新订单签证状态：id={}, visaStatus={}", id, visaStatus);
            
            orderService.updateVisaStatus(id, visaStatus);
            return Result.success();
        } catch (Exception e) {
            log.error("更新订单签证状态失败：", e);
            return Result.error("更新签证状态失败：" + e.getMessage());
        }
    }
}